package com.kaikeba.estack.bean;

import com.kaikeba.estack.web.constant.ErrorConstant;
import org.omg.CORBA.DATA_CONVERSION;

public class Message<T> {

    //状态码: 0成功，其他均为失败
    private int code;
    //消息内容
    private String msg;
    //消息所携带的一组数据
    private T data;


    public Message() {
    }


    public Message(ErrorConstant constant) {
        this(constant, null);
    }

    public Message(T data) {
        this(ErrorConstant.SUCCESS, data);
    }

    public Message(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Message(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Message(ErrorConstant constant, T data) {
        this.code = constant.getCode();
        this.msg = constant.getMsg();
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return this.code == 0;
    }

    public static <T> Message ok() {
        return new Message(ErrorConstant.SUCCESS);
    }

    public static <T> Message ok(T data) {
        return new Message(ErrorConstant.SUCCESS, data);
    }

    public static <T> Message build(ErrorConstant constant) {
        return new Message(constant);
    }

    public static Message error(String msg) {
        return new Message(ErrorConstant.ERROR.getCode(), msg);
    }
}
